<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex">
        <!-- 两个内容 -->
        <el-col :span="12">{{ nodeData.manager }}</el-col>
        <el-col :span="12">
          <!-- 下拉菜单 element -->
          <el-dropdown @command="command">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <!-- <el-dropdown-item command="add" @click.native="adddepart"> -->
              <el-dropdown-item command="add"> 添加子部门 </el-dropdown-item>
              <el-dropdown-item v-if="isRoot" command="edit">
                编辑部门
              </el-dropdown-item>
              <el-dropdown-item v-if="isRoot" command="del">
                删除部门
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import { delDepartmentApi } from '@/api/departments'
export default {
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {}
  },
  methods: {
    // adddepart() {},
    command(command) {
      if (command === 'add') {
        // 点击了添加部门
        this.$emit('opendialog', this.nodeData)
      }
      if (command === 'edit') {
        // 点击了编辑部门
        //把自己传过去  讲来调用自己
        this.$emit('editdialog', this.nodeData)
      }
      if (command === 'del') {
        // 删除是危险操作，要加二次确认
        this.$confirm('此操作将永久删除该部门, 是否继续？', '温馨提示')
          .then(async () => {
            // 同步的代码
            await delDepartmentApi(this.nodeData.id)
            // 删除完部门，重新渲染最新的部门列表
            this.$emit('getDepart')
            this.$message.success('删除成功！')
          })
          .catch(() => {
            console.log('点击了取消')
          })
        // 点击了删除部门
      }
    }
  }
}
</script>
